#include "PlayerInfoStats.h"
#include "Globals.h"




PlayerInfoStats::PlayerInfoStats()
{
	 m_life = 100;
	 m_shield = 100;
	 m_radioactivity = 0;
	 m_ammo = 20;
}


PlayerInfoStats::~PlayerInfoStats()
{
}

bool PlayerInfoStats::LoadInfoStats()
{
	
	char c[30];
	float x0, y0;
	float x1, y1;
	std::string m_slife, m_sshield, m_sradioactivity, m_sammo;
	float px, py;
	bool res;
	int size;
	

	res = true;
	
	m_font.LoadFonts();

	//life
	m_slife = itoa(m_life, c, 10);
	
	id_DL = glGenLists(1);
	glNewList(id_DL, GL_COMPILE);
	//life
		m_slife = itoa(m_life, c, 10);
		glBegin(GL_QUADS);
			px = 80;
			py = 16;
			if (m_life < 100)	{ size = 1; }
			else if (m_life<10) { size = 0; }
			else { size = 2; }
				for (int i = 0; i <= size; i++)
				{
				
					x0 = float(m_font.GetChar(m_slife[i]).first.first) / 320.0f;
					y0 = float(m_font.GetChar(m_slife[i]).first.second) / 192.0f;
					x1 = float(m_font.GetChar(m_slife[i]).second.first) / 320.0f;
					y1 = float(m_font.GetChar(m_slife[i]).second.second) / 192.0f;
							
					//Block x 320 32 0.1 y 192 32 
					glTexCoord2f(x0, y1);		glVertex2i(px, py);
					glTexCoord2f(x1, y1);		glVertex2i(px + 32, py);
					glTexCoord2f(x1, y0);		glVertex2i(px + 32, py + 32);
					glTexCoord2f(x0, y0);		glVertex2i(px, py + 32);
					px = px + 32;
				}
		glEnd();
		//Shield
		m_sshield = itoa(m_shield, c, 10);
		glBegin(GL_QUADS);
			px = 256;
			py = 16;
			if (m_shield < 100)	{ size = 1; }
			else if (m_shield<10) { size = 0; }
			else { size = 2; }
			for (int i = 0; i <= size; i++)
			{

				x0 = float(m_font.GetChar(m_sshield[i]).first.first) / 320.0f;
				y0 = float(m_font.GetChar(m_sshield[i]).first.second) / 192.0f;
				x1 = float(m_font.GetChar(m_sshield[i]).second.first) / 320.0f;
				y1 = float(m_font.GetChar(m_sshield[i]).second.second) / 192.0f;

				//Block x 320 32 0.1 y 192 32 
				glTexCoord2f(x0, y1);		glVertex2i(px, py);
				glTexCoord2f(x1, y1);		glVertex2i(px + 32, py);
				glTexCoord2f(x1, y0);		glVertex2i(px + 32, py + 32);
				glTexCoord2f(x0, y0);		glVertex2i(px, py + 32);
				px = px + 32;
			}	
		glEnd();

		//Redioactivity
		m_sradioactivity = itoa(m_radioactivity, c, 10);
		glBegin(GL_QUADS);
			px = 432;
			py = 16;
			if (m_radioactivity < 100)	{ size = 1; }
			else if (m_radioactivity<10) { size = 0; }
			else { size = 2; }
			for (int i = 0; i <= size; i++)
			{

				x0 = float(m_font.GetChar(m_sradioactivity[i]).first.first) / 320.0f;
				y0 = float(m_font.GetChar(m_sradioactivity[i]).first.second) / 192.0f;
				x1 = float(m_font.GetChar(m_sradioactivity[i]).second.first) / 320.0f;
				y1 = float(m_font.GetChar(m_sradioactivity[i]).second.second) / 192.0f;

				//Block x 320 32 0.1 y 192 32 
				glTexCoord2f(x0, y1);		glVertex2i(px, py);
				glTexCoord2f(x1, y1);		glVertex2i(px + 32, py);
				glTexCoord2f(x1, y0);		glVertex2i(px + 32, py + 32);
				glTexCoord2f(x0, y0);		glVertex2i(px, py + 32);
				px = px + 32;
			}
		glEnd();
		
		//AMMO
		m_sammo = itoa(m_ammo, c, 10);
		glBegin(GL_QUADS);
			px = 640;
			py = 16;
			if (m_ammo < 100)	{ size = 1; }
			else if (m_ammo <= 9 ) { size = 0; }
			else { size = 2; }
			for (int i = 0; i <= size; i++)
			{

				x0 = float(m_font.GetChar(m_sammo[i]).first.first) / 320.0f;
				y0 = float(m_font.GetChar(m_sammo[i]).first.second) / 192.0f;
				x1 = float(m_font.GetChar(m_sammo[i]).second.first) / 320.0f;
				y1 = float(m_font.GetChar(m_sammo[i]).second.second) / 192.0f;

				//Block x 320 32 0.1 y 192 32 
				glTexCoord2f(x0, y1);		glVertex2i(px, py);
				glTexCoord2f(x1, y1);		glVertex2i(px + 32, py);
				glTexCoord2f(x1, y0);		glVertex2i(px + 32, py + 32);
				glTexCoord2f(x0, y0);		glVertex2i(px, py + 32);
				px = px + 32;
			}
		glEnd();

	glEndList();

	return res;

}

void PlayerInfoStats::Draw(int tex_id)
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tex_id);
	glCallList(id_DL);
	glDisable(GL_TEXTURE_2D);
}


int PlayerInfoStats::GetLife()
{
	return m_life;
}

int PlayerInfoStats::GetShield()
{
	return m_shield;
}

int PlayerInfoStats::GetRadioactivity()
{
	return m_radioactivity;
}

int PlayerInfoStats::GetAmmo()
{
	return m_ammo;
}

void PlayerInfoStats::SetLife(bool damage, int valor)
{
	if (!damage)
	{ 
		if (m_life == 100){}
		else if (m_life > 90){m_life = 100;	}
		else{ m_life = m_life + valor; }
	}
	if (damage)
	{ 
		if (m_life == 0){}
		else { m_life = m_life - valor; }
	}
	LoadInfoStats();
}

void PlayerInfoStats::SetShield(bool damage, int valor)
{
	if (!damage)
	{
		if (m_shield == 100){}
		else if (m_shield < 90){ m_shield = 100; }
		else { m_shield = m_shield + valor; }
	}
	if (damage)
	{
		if (m_shield == 100){}
		else { m_shield = m_shield - valor;}
	}
	LoadInfoStats();
}

void PlayerInfoStats::SetRadioactivity(bool damage, int valor)
{
	if (damage)
	{
		if (m_radioactivity == 100){}
		else if (m_radioactivity > 90){ m_radioactivity = 100; }
		else{ m_radioactivity = m_radioactivity + valor; }
	}
	if (!damage){
		if (m_radioactivity == 0){}
		else { m_radioactivity = m_radioactivity - valor; }
	}
	LoadInfoStats();

}

void PlayerInfoStats::SetAmmo(bool incres, int valor)
{
	
	if (!incres){ m_ammo = m_ammo + valor; }
	if (incres){ m_ammo = m_ammo - valor; }
	LoadInfoStats();
}

void PlayerInfoStats::Dead()
{
	m_life = 0;
}


